﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Input;
using Bling.Core;
using Bling.WPF;
using Bling.WPF.Util;
using Bling.Slides;
using Bling.Util;
using Bling.Shaders;
using Bling.Graphics;
using Bling.WPF3D;
namespace Bling.TestP {
  using Bling.Shapes;
  using Bling.WPF.Custom;
  public class BezierTest : Canvas {
    public static readonly ImageSource NormalImage = "Resources/4573-normal.jpg".ImageFor(typeof(BezierTest));
    public static readonly ImageSource HeightImage = "Resources/4573-normal_DISP.jpg".ImageFor(typeof(BezierTest));
    public BezierTest() {
      var Self = this.Bl();

      var t0 = new SliderBl(Self) {
        LeftTop = new Double2Bl(0, 10),
        Minimum = 0 + .01,
        Value = 0.25,
        Width = Self.Width,
        
      };
      var t1 = new SliderBl(Self) {
        LeftTop = t0.LeftBottom,
        Minimum = t0.Value + .01,
        Maximum = 1 - .01,
        Value = 0.75,
        Width = Self.Width,
      };
      t0.Maximum = t1.Value - .01;
      Self.Background = Brushes.White;
      Self.ClipToBounds = true;
      Tex2D HeightField = new ImageBrushBl(HeightImage);
      Tex2D NormalMap = new ImageBrushBl(NormalImage);
      // make it scroll!
      {
        HeightField = HeightField.Distort(uv => new Double2Bl(uv.X, (uv.Y + 0).Frac));
        NormalMap = NormalMap.Distort(uv => new Double2Bl(uv.X, (uv.Y + 0).Frac));
        Lighting lighting = (new DiffuseMaterialCl()).ApplyLight(new PointLightBl() {
          Position = new Double3Bl(Self.Mouse.Position, 1000),
          //Position = new Double3Bl(Self.CenterSize, 450),
          Color = Colors.White,
        });

        // apply lighting to canvas.
        Self.Effect.Custom = lighting.Apply((HeightField)HeightField, (NormalMap)NormalMap,
                                      Self.Size.AddZ(0.015d), Self.CenterSize.AddZ(100d));
      }
      var r = new Random();
      var ThumbCanvas = new CanvasBl(Self) {
        Size = Self.Size - new Double2Bl(20, t1.Bottom), 
        Left = 10, Top = t1.Bottom, ClipToBounds = true,
      };
      var red = new ThumbBl(ThumbCanvas) {
        Background = Brushes.Red,
        Size = 20, ZIndex = 1,
        CanDrag = true,
        CenterPosition = r * ThumbCanvas.Size,
      };
      var blue = new ThumbBl(ThumbCanvas) {
        Background = Brushes.Blue,
        Size = 20,
        CanDrag = true,
        CenterPosition = r * ThumbCanvas.Size,
        ZIndex = 1,
      };
      var green = new ThumbBl(ThumbCanvas) {
        Background = Brushes.Green,
        Size = 20,
        CanDrag = true,
        CenterPosition = r * ThumbCanvas.Size,
        ZIndex = 1,
      };
      var purple = new ThumbBl(ThumbCanvas) {
        Background = Brushes.Purple,
        Size = 20, ZIndex = 1,
        CanDrag = true,
        CenterPosition = r * ThumbCanvas.Size,
      };
      if (true) {
        var canvas = new CanvasBl(Self) {
          LeftTop = t1.LeftBottom,
        };

        var lbl0 = new LabelBl(canvas) {
          LeftTop = 0,
          IsHitTestVisible = false,
          Font = { Weight = FontWeights.SemiBold, Size = 30, Family = "Courier New".Convert<FontFamily>() },
          Content = t0.Value.ToStringBl(100d) + ".Bezier(1, points).Bind = blue (" + blue.CenterPosition.ToStringBl(1d) + ")",
        };
        var lbl1 = new LabelBl(canvas) {
          LeftTop = lbl0.LeftBottom,
          Font = lbl0.Font,
          IsHitTestVisible = false,
          Content = t1.Value.ToStringBl(100d) + ".Bezier(2, points).Bind = green (" + green.CenterPosition.ToStringBl(1d) + ")",
        };
        canvas.RenderTransform[0].Scale = Self.Width / lbl0.Width.Max(lbl1.Width);

      }

      var segment = new BezierSegmentBl() {
        Point3 = purple,
      };
      new ThumbBl(ThumbCanvas) {
        Background = Brushes.Blue,
        Size = 10,
        CenterPosition = segment.Point1,
      };
      new ThumbBl(ThumbCanvas) {
        Background = Brushes.Green,
        Size = 10,
        CenterPosition = segment.Point2,
      };

      var points = segment.Points(red);
      t0.Value.Bezier(1, points).Bind = blue;
      t1.Value.Bezier(2, points).Bind = green;

      var shape = new PathShapeBl(ThumbCanvas) {
        Stroke = { Thickness = 2, Brush = Brushes.Black },
        Start = red,
        IsHitTestVisible = false,
      };
      shape.Segments.Add(segment);
      var shape2 = new PathShapeBl(ThumbCanvas) {
        Stroke = { Thickness = 1, Brush = Brushes.Black },
        Start = red,
        Segments = {
          AddMany = new PathSegmentBl[] {
            segment.Point1, segment.Point2, segment.Point3
          },
        },
        IsHitTestVisible = false,
      };


    }

  }
}